#include "GameMapSelect.h"
#include "../Game.h"
#include "../SGD Wrappers\SGD_InputManager.h"
#include "GameState.h"
#include "MainMenuState.h"
#include "GameMode.h"
#include "CharSelect.h"
#include "PracticeMode.h"
#include "DodgeBomb.h"

CGameMapSelect* CGameMapSelect::GetInstance(void)
{
	static CGameMapSelect s_Instance;
	return &s_Instance;
}

void CGameMapSelect::Enter(void)
{
	map1 = SGD::GraphicsManager::GetInstance()->LoadTexture("pictures/map1.png");
	map2 = SGD::GraphicsManager::GetInstance()->LoadTexture("pictures/map2.png");
	ballp1 = SGD::GraphicsManager::GetInstance()->LoadTexture("pictures/ballp1.png");
	ballp2 = SGD::GraphicsManager::GetInstance()->LoadTexture("pictures/ballp2.png");
	ballp3 = SGD::GraphicsManager::GetInstance()->LoadTexture("pictures/ballp3.png");
	ballp4 = SGD::GraphicsManager::GetInstance()->LoadTexture("pictures/ballp4.png");

}

void CGameMapSelect::Exit(void)
{
	m_nMap1 = 0;
	m_nMap2 = 0;

	m_bPlayer1choose = false;
	m_bPlayer2choose = false;
	m_bPlayer3choose = false;
	m_bPlayer4choose = false;

	SGD::GraphicsManager::GetInstance()->UnloadTexture(map1);
	SGD::GraphicsManager::GetInstance()->UnloadTexture(map2);
	SGD::GraphicsManager::GetInstance()->UnloadTexture(ballp1);
	SGD::GraphicsManager::GetInstance()->UnloadTexture(ballp2);
	SGD::GraphicsManager::GetInstance()->UnloadTexture(ballp3);
	SGD::GraphicsManager::GetInstance()->UnloadTexture(ballp4);
}

bool CGameMapSelect::Input(void)
{
	SGD::InputManager* pInput = SGD::InputManager::GetInstance();
	SGD::Vector LeftAnalog1 = pInput->GetLeftJoystick(0);
	SGD::Vector LeftAnalog2 = pInput->GetLeftJoystick(1);
	SGD::Vector LeftAnalog3 = pInput->GetLeftJoystick(2);
	SGD::Vector LeftAnalog4 = pInput->GetLeftJoystick(3);

	if (pInput->IsKeyPressed(SGD::Key::Escape) == true || pInput->IsButtonPressed(0, 1) == true)
	{
		Game::GetInstance()->ChangeState(CCharSelect::GetInstance());
		return true;
	}
	if (pInput->IsKeyPressed(SGD::Key::Down) == true ||
		pInput->IsDPadPressed(0, SGD::DPad::Down) == true
		)
	{
		++m_nCursor;

		// Wrap around
		if (m_nCursor > 2)
			m_nCursor = 0;
	}
	else if (pInput->IsKeyPressed(SGD::Key::Up) == true ||
		pInput->IsDPadPressed(0, SGD::DPad::Up) == true
		)
	{
		--m_nCursor;

		// Wrap around
		if (m_nCursor < 0)
			m_nCursor = 2;

	}

	if (m_bPlayer1choose == true && m_bPlayer2choose == true && m_bPlayer3choose == true && m_bPlayer4choose == true)
	{
		if (m_nMap1 > m_nMap2)		
			CGameState::GetInstance()->SetMap(true);
		else
			CGameState::GetInstance()->SetMap(false);		

		Game::GetInstance()->ChangeState(CGameState::GetInstance());
		return true;
	}


	float LX = LeftAnalog1.x;
	float LY = LeftAnalog1.y;

	float x1 = LX;
	float y1 = LY;

	float dx = 4 * x1;
	float dy = 4 * y1;

	SGD::Vector v = { dx, dy };
	v *= 64;
	m_vtVelocity1 = v;

	LX = LeftAnalog2.x;
	LY = LeftAnalog2.y;

	x1 = LX;
	y1 = LY;

	dx = 4 * x1;
	dy = 4 * y1;

	v = { dx, dy };
	v *= 64;
	m_vtVelocity2 = v;

	LX = LeftAnalog3.x;
	LY = LeftAnalog3.y;

	x1 = LX;
	y1 = LY;

	dx = 4 * x1;
	dy = 4 * y1;

	v = { dx, dy };
	v *= 64;
	m_vtVelocity3 = v;

	LX = LeftAnalog4.x;
	LY = LeftAnalog4.y;

	x1 = LX;
	y1 = LY;

	dx = 4 * x1;
	dy = 4 * y1;

	v = { dx, dy };
	v *= 64;
	m_vtVelocity4 = v;
	return true;
}

void CGameMapSelect::Update(float elapsedTime)
{
	SGD::InputManager* pInput = SGD::InputManager::GetInstance();

	m_ptPosition1 += m_vtVelocity1 * elapsedTime;
	m_ptPosition2 += m_vtVelocity2 * elapsedTime;
	m_ptPosition3 += m_vtVelocity3 * elapsedTime;
	m_ptPosition4 += m_vtVelocity4 * elapsedTime;
	KeepInWorld();

	if (map1rec.IsIntersecting(SGD::Rectangle{ m_ptPosition1, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(0, 0) == true)
	{
		// player 1 selected the first map 
		if (m_bPlayer1choose == false)
		{
			m_nMap1 += 1;
			m_bPlayer1choose = true;
		}
	}

	if (map2rec.IsIntersecting(SGD::Rectangle{ m_ptPosition1, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(0, 0) == true)
	{
		// player 1 selected the first map 
		if (m_bPlayer1choose == false)
		{
			m_nMap2 += 1;
			m_bPlayer1choose = true;
		}
	}

	if (map1rec.IsIntersecting(SGD::Rectangle{ m_ptPosition2, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(0, 0) == true)
	{
		// player 1 selected the first map 
		if (m_bPlayer2choose == false)
		{
			m_nMap1 += 1;
			m_bPlayer2choose = true;
		}
	}

	if (map2rec.IsIntersecting(SGD::Rectangle{ m_ptPosition2, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(0, 0) == true)
	{
		// player 1 selected the first map 
		if (m_bPlayer2choose == false)
		{
			m_nMap2 += 1;
			m_bPlayer2choose = true;
		}
	}

	if (map1rec.IsIntersecting(SGD::Rectangle{ m_ptPosition3, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(0, 0) == true)
	{
		// player 1 selected the first map 
		if (m_bPlayer3choose == false)
		{
			m_nMap1 += 1;
			m_bPlayer3choose = true;
		}
	}

	if (map2rec.IsIntersecting(SGD::Rectangle{ m_ptPosition3, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(0, 0) == true)
	{
		// player 1 selected the first map 
		if (m_bPlayer3choose == false)
		{
			m_nMap2 += 1;
			m_bPlayer3choose = true;
		}
	}

	if (map1rec.IsIntersecting(SGD::Rectangle{ m_ptPosition4, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(0, 0) == true)
	{
		// player 1 selected the first map 
		if (m_bPlayer4choose == false)
		{
			m_nMap1 += 1;
			m_bPlayer4choose = true;
		}
	}

	if (map2rec.IsIntersecting(SGD::Rectangle{ m_ptPosition4, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(0, 0) == true)
	{
		// player 1 selected the first map 
		if (m_bPlayer4choose == false)
		{
			m_nMap2 += 1;
			m_bPlayer4choose = true;
		}
	}
}

void CGameMapSelect::Render(void)
{
	const Font* pFont = Game::GetInstance()->GetFont();
	SGD::InputManager* pInput = SGD::InputManager::GetInstance();


	Game* pGame = Game::GetInstance();

	float darkOffset = pGame->DarkOff();
	float medOffset = pGame->MedOff();
	float lightOffset = pGame->LightOff();

	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetDarkSmoke(), { darkOffset, 0 }, 0.0f, { 0, 0 }, { 110, 255, 255, 255 });
	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetMedSmoke(), { medOffset, 0 }, 0.0f, { 0, 0 }, { 127, 255, 255, 255 });
	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetLightSmoke(), { lightOffset, 0 }, 0.0f, { 0, 0 }, { 135, 255, 255, 255 });

	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetDarkSmoke(), { darkOffset - 805, 0 }, 0.0f, { 0, 0 }, { 110, 255, 255, 255 });
	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetMedSmoke(), { medOffset - 805, 0 }, 0.0f, { 0, 0 }, { 127, 255, 255, 255 });
	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetLightSmoke(), { lightOffset - 805, 0 }, 0.0f, { 0, 0 }, { 135, 255, 255, 255 });

	pFont->Draw("Map Select", { 40, 1 }, 1, { 255, 255, 255 });

	SGD::GraphicsManager::GetInstance()->DrawTextureSection(map1, { 20, 100 }, SGD::Rectangle{ 0, 0, 800, 600 }, 0.0f, {}, { 255, 255, 255 }, { 0.4f, 0.4f });
	SGD::GraphicsManager::GetInstance()->DrawTextureSection(map2, { 20, 350 }, SGD::Rectangle{ 0, 0, 800, 600 }, 0.0f, {}, { 255, 255, 255 }, { 0.4f, 0.4f });

	//pFont->Draw("This is our \nbasic map", { 360, 240 }, 0.8f, { 255, 255, 255 });

	if (pInput->IsControllerConnected(0))
		SGD::GraphicsManager::GetInstance()->DrawTexture(ballp1, { m_ptPosition1.x, m_ptPosition1.y }, 0, { 0, 0 }, {}, { 0.25f, 0.25f });
	else
		m_bPlayer1choose = true;


	if (pInput->IsControllerConnected(1))
		SGD::GraphicsManager::GetInstance()->DrawTexture(ballp2, { m_ptPosition2.x, m_ptPosition2.y }, 0, { 0, 0 }, {}, { 0.25f, 0.25f });
	else
		m_bPlayer2choose = true;


	if (pInput->IsControllerConnected(2))
		SGD::GraphicsManager::GetInstance()->DrawTexture(ballp3, { m_ptPosition3.x, m_ptPosition3.y }, 0, { 0, 0 }, {}, { 0.25f, 0.25f });
	else
		m_bPlayer3choose = true;


	if (pInput->IsControllerConnected(3))
		SGD::GraphicsManager::GetInstance()->DrawTexture(ballp4, { m_ptPosition4.x, m_ptPosition4.y }, 0, { 0, 0 }, {}, { 0.25f, 0.25f });
	else
		m_bPlayer4choose = true;


}

void CGameMapSelect::KeepInWorld()
{
	//dont go too far left
	if (SGD::Rectangle{ m_ptPosition1, SGD::Size{ 64, 64 } }.left <= 0)
		m_ptPosition1.x = 1;

	//dont go too far up
	if (SGD::Rectangle{ m_ptPosition1, SGD::Size{ 64, 64 } }.top <= 0)
		m_ptPosition1.y = 1;

	//dont go too far right
	if (SGD::Rectangle{ m_ptPosition1, SGD::Size{ 64, 64 } }.left + SGD::Size{ 64, 64 }.width > Game::GetInstance()->GetWorldWidth())
		m_ptPosition1.x = Game::GetInstance()->GetWorldWidth() - SGD::Size{ 64, 64 }.width;

	//dont go too far down
	if (SGD::Rectangle{ m_ptPosition1, SGD::Size{ 64, 64 } }.bottom > Game::GetInstance()->GetWorldHeight())
		m_ptPosition1.y = Game::GetInstance()->GetWorldHeight() - SGD::Size{ 64, 64 }.height;

	//dont go too far left
	if (SGD::Rectangle{ m_ptPosition2, SGD::Size{ 64, 64 } }.left <= 0)
		m_ptPosition2.x = 1;

	//dont go too far up
	if (SGD::Rectangle{ m_ptPosition2, SGD::Size{ 64, 64 } }.top <= 0)
		m_ptPosition2.y = 1;

	//dont go too far right
	if (SGD::Rectangle{ m_ptPosition2, SGD::Size{ 64, 64 } }.left + SGD::Size{ 64, 64 }.width > Game::GetInstance()->GetWorldWidth())
		m_ptPosition2.x = Game::GetInstance()->GetWorldWidth() - SGD::Size{ 64, 64 }.width;

	//dont go too far down
	if (SGD::Rectangle{ m_ptPosition2, SGD::Size{ 64, 64 } }.bottom > Game::GetInstance()->GetWorldHeight())
		m_ptPosition2.y = Game::GetInstance()->GetWorldHeight() - SGD::Size{ 64, 64 }.height;

	//dont go too far left
	if (SGD::Rectangle{ m_ptPosition3, SGD::Size{ 64, 64 } }.left <= 0)
		m_ptPosition3.x = 1;

	//dont go too far up
	if (SGD::Rectangle{ m_ptPosition3, SGD::Size{ 64, 64 } }.top <= 0)
		m_ptPosition3.y = 1;

	//dont go too far right
	if (SGD::Rectangle{ m_ptPosition3, SGD::Size{ 64, 64 } }.left + SGD::Size{ 64, 64 }.width > Game::GetInstance()->GetWorldWidth())
		m_ptPosition3.x = Game::GetInstance()->GetWorldWidth() - SGD::Size{ 64, 64 }.width;

	//dont go too far down
	if (SGD::Rectangle{ m_ptPosition3, SGD::Size{ 64, 64 } }.bottom > Game::GetInstance()->GetWorldHeight())
		m_ptPosition3.y = Game::GetInstance()->GetWorldHeight() - SGD::Size{ 64, 64 }.height;

	//dont go too far left
	if (SGD::Rectangle{ m_ptPosition4, SGD::Size{ 64, 64 } }.left <= 0)
		m_ptPosition4.x = 1;

	//dont go too far up
	if (SGD::Rectangle{ m_ptPosition4, SGD::Size{ 64, 64 } }.top <= 0)
		m_ptPosition4.y = 1;

	//dont go too far right
	if (SGD::Rectangle{ m_ptPosition4, SGD::Size{ 64, 64 } }.left + SGD::Size{ 64, 64 }.width > Game::GetInstance()->GetWorldWidth())
		m_ptPosition4.x = Game::GetInstance()->GetWorldWidth() - SGD::Size{ 64, 64 }.width;

	//dont go too far down
	if (SGD::Rectangle{ m_ptPosition4, SGD::Size{ 64, 64 } }.bottom > Game::GetInstance()->GetWorldHeight())
		m_ptPosition4.y = Game::GetInstance()->GetWorldHeight() - SGD::Size{ 64, 64 }.height;

}
